package com.song.ejf.action;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionSupport;
import com.song.ejf.page.Page;
import com.song.ejf.pojo.SResourceGroup;
import com.song.ejf.service.ResourceGroupService;
import com.song.ejf.util.DateUtil;
import com.song.ejf.util.UUIDUtil;

public class ResourceGroupAction extends ActionSupport {

	private static final long serialVersionUID = -6609393044471160489L;
	Log log = LogFactory.getLog(ResourceGroupAction.class);
	public SResourceGroup resourceGroup;
	public String checkItems;
	private List<Map<String,String>> inGroupRes;
	private List<Map<String,String>> noGroupRes;
	public Page page;
	@Autowired()
	public ResourceGroupService resourceGroupService;
	/**
	 * 准备修改一个功能组的相关功能.
	 * @return
	 */
	public String pConfigResGroup(){
		try {
			inGroupRes = resourceGroupService.loadResGroupMapByRgID(resourceGroup.getRGId());
			noGroupRes = resourceGroupService.loadAllNoGroupRes();
		} catch (Exception e) {
			log.error("跳转到配置资源组页面异常.",e);
		}
		return "pConfigResGroup";
	}
	public String configResGroup(){
		try {
			boolean flag = resourceGroupService.updateConfigResGroup(resourceGroup.getRGId(), checkItems);
			if(flag){
				addActionMessage("指派功能成功!");
			}else{
				addActionMessage("指派功能失败!");
			}
		} catch (Exception e) {
			log.error("配置资源组异常.",e);
		}
		
		return "configResGroup";
	}
	/** 
	 * 准备添加资源组
	 * @return
	 */
	public String pAddResourceGroup(){
		return "pAddResourceGroup";
	}
	/**
	 * 添加资源组.
	 * @return
	 */
	public String addResourceGroup(){
		try {
			if(resourceGroup != null){
				resourceGroup.setLastModifyTime(DateUtil.formatDate());
				resourceGroup.setRGId(UUIDUtil.makeUUID());
				resourceGroupService.saveOrUpdateResourceGroup(resourceGroup);
				addActionMessage("添加资源组成功!");
			}else{
				addActionMessage("添加资源组失败!");
			}
		} catch (Exception e) {
			log.error("添加资源组异常.",e);
		}
		return "addResourceGroup";
	}
	/**
	 * 列出所有资源组
	 * @return
	 */
	public String listResourceGroup(){
		try {
			if(page == null){
				page = new Page();
			}
			if(resourceGroup == null){
				resourceGroup = new SResourceGroup();
			}
			resourceGroupService.loadResourceGroupPage(page, resourceGroup.getResourceGroupName());
		} catch (Exception e) {
			log.error("资源组列表异常.",e);
		}
		return "listResourceGroup";
	}
	/**
	 * 删除资源组
	 * @return
	 */
	public String delResourceGroup(){
		try {
			if(StringUtils.isBlank(checkItems)){
				addActionMessage("删除资源组失败!");
				return "delResource";
			}
			boolean flag = resourceGroupService.delResourceGroup(checkItems.split(","));
			if(flag){
				addActionMessage("删除资源组成功!");
			}else{
				addActionMessage("删除资源组失败!");
			}
		} catch (Exception e) {
			log.error("删除资源组异常.",e);
		}
		return "delResourceGroup";
	}
	/**
	 * 准备编辑资源组
	 * @return
	 */
	public String pEditResourceGroup(){
		try {
			resourceGroup = resourceGroupService.findResourceGroupById(resourceGroup.getRGId());
		} catch (Exception e) {
			log.error("跳转到编辑资源组页面异常.",e);
		}
		return "pEditResourceGroup";
	}
	/**
	 * 编辑资源组
	 * @return
	 */
	public String editResourceGroup(){
		try {
			if(resourceGroup != null){
				resourceGroup.setLastModifyTime(DateUtil.formatDate());
				resourceGroupService.saveOrUpdateResourceGroup(resourceGroup);
				addActionMessage("修改资源组成功!");
			}else{
				addActionMessage("修改资源组失败!");
			}
		} catch (Exception e) {
			log.error("编辑资源组异常.",e);
		}
		return "editResourceGroup";
	}
	
	////////////////////////////////////////////////////////

	public Page getPage() {
		return page;
	}

	public SResourceGroup getResourceGroup() {
		return resourceGroup;
	}
	public void setResourceGroup(SResourceGroup resourceGroup) {
		this.resourceGroup = resourceGroup;
	}
	public void setPage(Page page) {
		this.page = page;
	}
	public String getCheckItems() {
		return checkItems;
	}
	public void setCheckItems(String checkItems) {
		this.checkItems = checkItems;
	}
	public List<Map<String, String>> getInGroupRes() {
		return inGroupRes;
	}
	public void setInGroupRes(List<Map<String, String>> inGroupRes) {
		this.inGroupRes = inGroupRes;
	}
	public List<Map<String, String>> getNoGroupRes() {
		return noGroupRes;
	}
	public void setNoGroupRes(List<Map<String, String>> noGroupRes) {
		this.noGroupRes = noGroupRes;
	}
}
